<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - node playground</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" href="fonts/open-sans/open-sans.css" type="text/css"/>
		<link rel="stylesheet" href="fonts/tabler-icons/tabler-icons.min.css" type="text/css"/>
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			body {
				overflow: hidden;
				width: 100%;
				height: 100%;
			}
			.renderer {
				position: absolute;
				top: 0;
				left: 0;
				height: 100%;
				width: 100%;
			}
			flow {
				position: absolute;
				top: 0;
				left: 0;
				height: 100%;
				width: 100%;
				box-shadow: inset 0 0 20px 0px #000000;
				pointer-events: none;
			}
			flow f-canvas {
				pointer-events: auto;
			}
			flow f-canvas:not(.focusing) {
				background: #191919ed;
			}
		</style>
	</head>
	<body>

		<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/",
					"three/nodes": "./jsm/nodes/Nodes.js"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';
			import * as Nodes from 'three/nodes';

			import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';

			import { NodeEditor } from 'three/addons/node-editor/NodeEditor.js';
			import { MeshEditor } from 'three/addons/node-editor/scene/MeshEditor.js';
			import { StandardMaterialEditor } from 'three/addons/node-editor/materials/StandardMaterialEditor.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';

			let camera, scene, renderer;
			let model;
			let nodeEditor;

			init();
			animate();

			function init() {

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.01, 100 );
				camera.position.set( 0.0, 3, 4 * 3 );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x333333 );

				// Lights

				const topLight = new THREE.PointLight( 0xF4F6F0, 1, 100 );
				topLight.power = 4500;
				topLight.position.set( 0, 10, 10 );
				scene.add( topLight );

				const backLight = new THREE.PointLight( 0x0c1445, 1, 100 );
				backLight.power = 1000;
				backLight.position.set( - 1, .2, - 2.6 );
				scene.add( backLight );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				document.body.appendChild( renderer.domElement );
				renderer.outputEncoding = THREE.sRGBEncoding;
				renderer.toneMapping = THREE.LinearToneMapping;
				renderer.toneMappingExposure = 1;
				renderer.physicallyCorrectLights = true;

				renderer.domElement.className = 'renderer';

				//

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 5;
				controls.maxDistance = 30;

				window.addEventListener( 'resize', onWindowResize );

				initEditor();

				onWindowResize();

			}

			function initEditor() {

				nodeEditor = new NodeEditor( scene );

				const reset = () => {

					const meshEditor = new MeshEditor( model );
					const materialEditor = new StandardMaterialEditor();

					nodeEditor.add( meshEditor );
					nodeEditor.add( materialEditor );
					nodeEditor.centralizeNode( meshEditor );

					const { x, y } = meshEditor.getPosition();

					meshEditor.setPosition( x + 250, y );
					materialEditor.setPosition( x - 250, y );

					meshEditor.material.connect( materialEditor );

				};

				nodeEditor.addEventListener( 'new', reset );

				document.body.appendChild( nodeEditor.domElement );

				const loaderFBX = new FBXLoader();
				loaderFBX.load( 'models/fbx/stanford-bunny.fbx', ( object ) => {

					const defaultMaterial = new Nodes.MeshBasicNodeMaterial();
					defaultMaterial.colorNode = new Nodes.UniformNode( 0 );

					const sphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 32, 16 ), defaultMaterial );
					sphere.name = 'Sphere';
					sphere.position.set( 5, 0, - 5 );
					scene.add( sphere );

					const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), defaultMaterial );
					box.name = 'Box';
					box.position.set( - 5, 0, - 5 );
					scene.add( box );

					const defaultPointsMaterial = new Nodes.PointsNodeMaterial();
					defaultPointsMaterial.colorNode = new Nodes.UniformNode( 0 );
					defaultPointsMaterial.size = 0.01;

					const torusKnot = new THREE.Points( new THREE.TorusKnotGeometry( 1, .3, 100, 16 ), defaultPointsMaterial );
					torusKnot.name = 'Torus Knot ( Points )';
					torusKnot.position.set( 0, 0, - 5 );
					scene.add( torusKnot );

					model = object.children[ 0 ];
					model.position.set( 0, 0, .1 );
					model.scale.setScalar( .01 );
					model.material = defaultMaterial;
					scene.add( model );

					reset();

				} );

			}

			function onWindowResize() {

				const width = window.innerWidth;
				const height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );

				nodeEditor.setSize( width, height );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				nodeFrame.update();

				render();

			}

			function render() {

				//if ( model ) model.rotation.y = performance.now() / 5000;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
